/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
function openpopup(url) {
 fenster=window.open(url, "_blank", "width=600,height=400,status=yes,scrollbars=yes,resizable=yes, toolbar=1, menubar=1, location=1");
 fenster.focus();
 return false;
}

function validateForm(form){
    for (var i=0; i < form.elements.length; i++) {
	var temp = form.elements[i].value;
	var alphanumeric = ' !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
	
	if(form.elements[i].className == "required parameter")
	{
		valid = /(^[\x00-\x7F]+$)|^$/.test(temp);
			if(!valid){
	    	alert("The field "+form.elements[i].name+" contains illegal characters: "+form.elements[i].value+"\n \n Use only combinations of the following characters: "+alphanumeric);
	    	document.Formular.elements[i].focus();
	    	return false
	 	} //end if(!valid)
    }//end if (required parameter) 
    }//end for
    return valid;
}


function numValues() {
var study_type = document.Formular.study_type.value;
var num = 1;
var regx = 0;

// NON-ASCII character check for all input fields
if (!validateForm(document.Formular)){
	return false;
}

// ALL STUDY_TYPES!!
// COLLECTION DATE
var result = document.Formular.collection_date.value.match(/^[0-9]{4}(\-(0[1-9]|1[012])(\-(0[1-9]|[12][0-9]|3[01])(.(0[0-9]|1[0-9]|2[0-3])\:([0-5][0-9])(\:[0-5][0-9])?([\+\-][0-9]{2}\:[0-9]{2})?)?)?)?$/);
if(!document.Formular.collection_date.value == "") {
	if(!result){
		alert("Please enter a valid date in the form (YYYY-MM-DDTHH:MM:SS), e.g.: 2008-06-14T23:40. This is not correct: "+document.Formular.collection_date.value);
		document.Formular.collection_date.focus();
		return false;
	}
}
// LAT_LON all
var lat_lon = document.Formular.lat_lon.value;
var lat_result1 = lat_lon.match(/^((((90((\.|\,)[0]{1,5})?)|([0-8]?[0-9]((\.|\,)[0-9]{0,5})?))\s?[NSns\+\-])|([NSns\+\-]\s?((90((\.|\,)[0]{1,5})?)|([0-8]?[0-9]((\.|\,)[0-9]{0,5})?))))\s{1,}((((180((\.|\,)[0]{1,5})?)|((1[0-7][0-9]|0?[0-9]{1,2})((\.|\,)[0-9]{0,5})?))\s?[EWew\+\-])|([EWew\+\-]\s?((180((\.|\,)[0]{1,5})?)|((1[0-7][0-9]|0?[0-9]{1,2})((\.|\,)[0-9]{0,5})?))))$/);
if(!document.Formular.lat_lon.value == ""){
	if((!lat_result1)){
		alert("Please enter the right format latitude longitude. E.g. N35.56123 W123.98732");
		document.Formular.lat_lon.focus();
		return false;
	}
}
// PCR PRIMER mimarks_s/c FWD:{dna} REV:{dna}  FWD\:[A-Za-z]+\sREV\:[A-Za-z]+
if(study_type == "mimarks_s" || study_type == "mimarks_c"){
	var primers = document.Formular.pcr_primers.value;
	primers = primers.toLowerCase();
	var primer_result = primers.match((/^(fwd\:)?[a-z]+\s(rev\:)?[a-z]+$/));
	if(!document.Formular.pcr_primers.value == ""){
		if(!primer_result){
			alert("Please enter a right primer sequenze (e.g. FWD:{dna} REV:{dna}).");
			document.Formular.pcr_primers.focus();
			return false;
		}
	}
}// end if(study_type == "mimarks_s" "mimarks_c")
// LIB READS SEQD, LIB SIZE mimarks_S me
if(study_type == "mimarks_s" || study_type == "me"){
	for(i = 0; i < document.Formular.lib_reads_seqd.value.length; ++i){
		if(document.Formular.lib_reads_seqd.value.charAt(i) < "0" ||
			document.Formular.lib_reads_seqd.value.charAt(i) > "9"){
				num = -1;
		}
		if(num == -1) {
			alert("Please enter numbers in the library reads sequenced field!");
			document.Formular.lib_reads_seqd.focus();
			return false;
		}
	}
	for(i = 0; i < document.Formular.lib_size.value.length; ++i){
		if(document.Formular.lib_size.value.charAt(i) < "0" ||
			document.Formular.lib_size.value.charAt(i) > "9"){
				num = -1;
		}
		if(num == -1) {
			alert("Please enter numbers in the library size field!");
			document.Formular.lib_size.focus();
			return false;
		}
	}
}// end if(study_type == "mimarks_s" "me")
// NUM REPLICONS ok vi ba eu
if(study_type == "vi" || study_type == "ba" || study_type == "eu"){
	for(i = 0; i < document.Formular.num_replicons.value.length; ++i){
		if (document.Formular.num_replicons.value.charAt(i) < "0" ||
			document.Formular.num_replicons.value.charAt(i) > "9"){
				num = -1;
		}
		if (num == -1) {
			alert("Please enter numbers (0-9) in the number of replicons field!");
			document.Formular.num_replicons.focus();
			return false;
		}
	}
}// end if(study_type == "vi" "ba" "eu")
// EXTRACHROM ELEMENTS ok mimarks_c eu ba orga
if(study_type == "mimarks_c" || study_type == "ba" || study_type == "eu" || study_type == "orga"){
	for(i = 0; i < document.Formular.extrachrom_elements.value.length; ++i){
		if(document.Formular.extrachrom_elements.value.charAt(i) < "0" ||
			document.Formular.extrachrom_elements.value.charAt(i) > "9"){
				num = -1;
		}
		if(num == -1) {
			alert("Please enter numbers (0-9) in the extrachromosomal elements field!");
			document.Formular.extrachrom_elements.focus();
			return false;
		}
	}
}// end if(study_type == "mimarks_c" "ba" "eu" "orga")
}/*end function numValues()*/